home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_screen_aa.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  2.6 KB  |  84 lines

  1. /*
  2.  * screen_aa.sl -- RenderMan compatible shader for a metalic screen.
  3.  *
  4.  * DESCRIPTION:
  5.  *   Makes a surface that looks like a metal screen.  Strips of metal run
  6.  *   parallel to lines of s and t.  You can adjust the Ka, Kd, Ks, etc.
  7.  *   to change the material appearance.  This texture antialiases pretty
  8.  *   well, even with only one sample per pixel.
  9.  *
  10.  * PARAMETERS:
  11.  *   Ka, Kd, Ks, roughness, specularcolor - work just like the plastic shader
  12.  *   frequency - how many cycles of screen in st space
  13.  *   density - how much of each cycle is opaque?
  14.  *
  15.  * AUTHOR: written by Larry Gritz
  16.  *
  17.  * last modified  31 Jan 1994 by Larry Gritz
  18.  *
  19.  *
  20.  * The RenderMan (R) Interface Procedures and RIB Protocol are:
  21.  *     Copyright 1988, 1989, Pixar.  All rights reserved.
  22.  * RenderMan (R) is a registered trademark of Pixar.
  23.  */
  24.  
  25. #define boxstep(a,b,x) (clamp(((x)-(a))/((b)-(a)),0,1))
  26. #define MINFILTERWIDTH 1.0e-7
  27.  
  28.  
  29.  
  30. surface k3d_screen_aa(float Ka = 1, Kd = 0.75, Ks = 0.4, roughness = 0.1;
  31.               color specularcolor = 1;
  32.               float density = 0.25, frequency = 20;)
  33. {
  34.   normal Nf;            /* Forward facing Normal vector */
  35.   vector IN;            /* normalized incident vector */
  36.   float d;            /* Density at the sample point */
  37.   float ss, tt;            /* s,t, parameters in phase */
  38.   float swidth, twidth, GWF, w, h;
  39.  
  40.   /* Compute a forward facing normal */
  41.   IN = normalize(I);
  42.   Nf = faceforward(normalize(N), I);
  43.  
  44.   /* Determine how wide in s-t space one pixel projects to */
  45.   swidth = max(abs(Du(s) * du) + abs(Dv(s) * dv), MINFILTERWIDTH) * frequency;
  46.   twidth = max(abs(Du(t) * du) + abs(Dv(t) * dv), MINFILTERWIDTH) * frequency;
  47.  
  48.   /* Figure out where in the pattern we are */
  49.   ss = mod(frequency * s, 1);
  50.   tt = mod(frequency * t, 1);
  51.  
  52.   /* Figure out where the strips are. Do some simple antialiasing. */
  53.   GWF = density * 0.5;
  54.   if(swidth >= 1)
  55.     w = 1 - 2 * GWF;
  56.   else
  57.     w =
  58.       clamp(boxstep(GWF - swidth, GWF, ss), max(1 - GWF / swidth, 0),
  59.         1) - clamp(boxstep(1 - GWF - swidth, 1 - GWF, ss), 0,
  60.                2 * GWF / swidth);
  61.   if(twidth >= 1)
  62.     h = 1 - 2 * GWF;
  63.   else
  64.     h =
  65.       clamp(boxstep(GWF - twidth, GWF, tt), max(1 - GWF / twidth, 0),
  66.         1) - clamp(boxstep(1 - GWF - twidth, 1 - GWF, tt), 0,
  67.                2 * GWF / twidth);
  68.   /* This would be the non-antialiased version:
  69.    *    w = step (GWF,ss) - step(1-GWF,ss);
  70.    *    h = step (GWF,tt) - step(1-GWF,tt);
  71.    */
  72.   d = 1 - w * h;
  73.  
  74.   Oi = d;
  75.   if(d > 0)
  76.     {
  77.       Ci =
  78.     Oi * (Cs * (Ka * ambient() + Kd * diffuse(Nf)) +
  79.           specularcolor * Ks * specular(Nf, -IN, roughness));
  80.     }
  81.   else
  82.     Ci = 0;
  83. }
  84.